package com.leps.nq.sys.controller;

import com.leps.nq.common.aspect.Log;
import com.leps.nq.common.enums.BusinessType;
import com.leps.nq.common.vo.R;
import com.leps.nq.common.vo.RBuilder;
import com.leps.nq.sys.entity.SysMenu;
import com.leps.nq.sys.entity.SysRole;
import com.leps.nq.sys.service.ISysMenuService;
import com.leps.nq.sys.service.ISysRoleService;
import com.leps.nq.sys.vo.CreateRoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author hex
 * @since 2022-05-20
 */
@RestController
@RequestMapping("/sys/sys-role")
@Api(tags = "角色管理")
public class SysRoleController {
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private ISysMenuService menuService;

    @ApiOperation("获取系统所有角色")
    @GetMapping
    public R<List<SysRole>> list() {
        List<SysRole> roles = this.roleService.selectRoles();
        return RBuilder.ok(roles);
    }

    @ApiOperation("根据ID获取角色基本信息")
    @GetMapping("/{roleId}")
    public R<SysRole> getOne(@PathVariable Long roleId) {
        SysRole role = this.roleService.selectById(roleId);
        return RBuilder.ok(role);
    }

    @ApiOperation("获取角色拥有的菜单")
    @GetMapping("/ownMenus")
    public R<List<SysMenu>> ownMenus(Long roleId) {
        List<SysMenu> menus = this.roleService.selectMenusByRoleId(roleId);
        return RBuilder.ok(menus);
    }

    @ApiOperation("创建角色")
    @PostMapping
    @Log(title = "系统角色", businessType = BusinessType.INSERT)
    public R save(@RequestBody CreateRoleVo createRoleVo) {
        boolean b = this.roleService.createRole(createRoleVo);
        return RBuilder.ofBoolean(b);
    }

    @ApiOperation("为角色分配菜单")
    @PutMapping("/grantMenus")
    @Log(title = "系统角色", businessType = BusinessType.OTHER)
    public R grantMenus(Long roleId, Long[] menuIds) {
        boolean b = this.roleService.grantMenusToRole(roleId, menuIds);
        return RBuilder.ofBoolean(b);
    }

    @ApiOperation("更新角色信息")
    @PutMapping
    @Log(title = "系统角色", businessType = BusinessType.UPDATE)
    public R update(@RequestBody SysRole sysRole) {
        boolean b = this.roleService.updateRole(sysRole);
        return RBuilder.ofBoolean(b);
    }

    @ApiOperation("改变角色状态：0正常 1禁用")
    @PutMapping("/changeStatus")
    @Log(title = "系统角色", businessType = BusinessType.UPDATE)
    public R changeStatus(Long roleId, String status) {
        boolean b = this.roleService.changeStatus(roleId, status);
        return RBuilder.ofBoolean(b);
    }

    @ApiOperation("删除角色")
    @DeleteMapping
    @Log(title = "系统角色", businessType = BusinessType.DELETE)
    public R remove(@RequestBody Long[] roleIds) {
        boolean b = this.roleService.removeRole(roleIds);
        return RBuilder.ofBoolean(b);
    }


}
